package com.huatai.bpmn.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huatai.bpmn.entity.vo.DataVo;
import com.huatai.bpmn.entity.vo.PageVo;
import com.huatai.bpmn.service.BasicLoadService;
import com.huatai.common.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  表格数据存储表
 * </p>
 *
 * @author yxy
 * @since 2023-07-07
 */
@RestController
@RequestMapping("/basicLoad")
@Api(value = "新-表格数据存储表", tags = "新-表格数据存储表")
public class BasicLoadController {

	@Autowired
	private BasicLoadService basicLoadService;

	/**
	 * 获取项目库数据
	 *
	 * @return
	 */
	@PostMapping("/libraryList")
	@ApiOperation("获取项目库数据 data参数为pgIndex，pgSize，place")
	public R pageList(@RequestBody JSONObject data) {
		IPage page = basicLoadService.pageList(data);
		return R.data(page);
	}




	/**
	 * 表单数据新增多条数据  data参数place，businessKey（“审批id”）,表单对应key-value
	 *	@param data 表单数据新增多条数据  data参数place，businessKey（“审批id”）,表单对应key-value
	 * @return
	 */
	/*@PostMapping("/addList")
	@ApiOperation("表单数据新增多条数据  data参数place，businessKey（“审批id”）,表单对应key-value")
	public R addList(@RequestBody List<JSONObject> data,@RequestParam String businessKey) {
		boolean flag = basicLoadService.addList(data,businessKey);
		return flag ? R.success("新增成功") : R.fail("新增失败");
	}*/





	/**
	 * 获取项目库数据
	 *
	 * @return
	 */
	@PostMapping("/pageListByBusinessKey")
	@ApiOperation("根据businessKey获取数据，data参数为place,businessKey")
	public R pageListByBusinessKey(@RequestBody JSONObject data) {
		List<JSONObject> list=basicLoadService.pageListByBusinessKey(data);
		return R.data(list);
	}

	/**
	 * 表单数据修改多条数据
	 *	@param data 表单数据新增多条数据  data参数place，businessKey（“审批id”）,表单对应key-value
	 * @return
	 */
	@PostMapping("/updateList")
	@ApiOperation("表单数据修改多条数据  data参数place，businessKey（“审批id”）,表单对应key-value")
	public R updateList(@RequestBody List<JSONObject> data,@RequestParam String businessKey) {
		boolean flag = basicLoadService.updateList(data,businessKey);
		return flag ? R.success("修改成功") : R.fail("修改失败");
	}

	/*@ApiOperation("创建任务--弃用")
	@PostMapping("/createTask")
	public R createTask(@RequestParam String externalParameterId,@RequestParam String deployId){
		HashMap<String, Object> columns = basicLoadService.createTask(externalParameterId,deployId);
		return R.data(columns);
	}*/

	/*@ApiOperation("已发任务查询--弃用")
	@GetMapping("/taskQuery/{businessKey}")
	public R taskQuery(@PathVariable String businessKey){
		HashMap<String, Object> map = basicLoadService.taskQuery(businessKey);
		return R.data(map);
	}*/

	@ApiOperation("项目数据查询")
	@PostMapping("/pageDataList")
	public R pageDataList(@RequestBody DataVo dataVo){
		JSONObject jsonObject = basicLoadService.pageDataList(dataVo);
		return R.data(jsonObject);
	}

	@ApiOperation("数据导出")
	@GetMapping("/dowLoadExcel/{myKey}")
	public R dowLoadExcel(@PathVariable String myKey,HttpServletResponse response){
		String fileName = basicLoadService.dowLoadExcel(myKey,response);
		return R.data(fileName);
	}
	@ApiOperation("模板导出")
	@GetMapping("/template/{myKey}")
	public R template(@PathVariable String myKey, HttpServletResponse response){
		String fileName = basicLoadService.template(myKey,response);
		return R.data(fileName);
	}

	@ApiOperation("数据导入")
	@PostMapping("/importExcel")
	public R importExcel(String myKey, MultipartFile file){
		return R.data(basicLoadService.importExcel(myKey,file));
	}

	/**
	 * 表单数据删除
	 *	@param businessKey
	 * @return
	 */
	@PostMapping("/remove")
	@ApiOperation("表单数据删除")
	public R remove(String businessKey) {
		boolean flag = basicLoadService.remove(businessKey);
		return flag ? R.success("删除成功") : R.fail("删除失败");
	}

	/**
	 * 数据转入实施库
	 *	@param businessKey
	 * @return
	 */
	@PostMapping("/shift")
	@ApiOperation("转库")
	public R shift(String businessKey,String myKey) {
		boolean flag = basicLoadService.shiftSS(businessKey,myKey);
		return flag ? R.success("操作成功") : R.fail("操作失败");
	}

	/**
	 * 查看详情
	 *	@param businessKey
	 * @return
	 */
	@PostMapping("/getInfo")
	@ApiOperation("查看详情")
	public R getInfo(String myKey,String businessKey) {
		HashMap<String, Object> map = basicLoadService.getInfo(myKey, businessKey);
		return R.data(map);
	}



	/**
	 * 表格数据删除
	 *	@param flowId
	 * @return
	 */
	@PostMapping("/removeByFlowId")
	@ApiOperation("根据flow_id删除表格数据")
	public R removeByFlowId(String flowId) {
		boolean flag = basicLoadService.removeByFlowId(flowId);
		return flag ? R.success("删除成功") : R.fail("删除失败");
	}

	/**
	 * 表单数据新增
	 *	@param data 表单数据+表单标识+流程id businessKey
	 * @return
	 */
	@PostMapping("/add")
	@ApiOperation("表格数据新增")
	public R add(@RequestBody JSONObject data,@RequestParam String place,@RequestParam String businessKey) {
		boolean flag = basicLoadService.add(data,place,businessKey);
		return flag ? R.success("新增成功") : R.fail("新增失败");
	}

	/**
	 *
	 * @param data 表单数据+表单标识
	 * @return
	 */
	@PostMapping("/update")
	@ApiOperation("表格数据修改")
	public R update(@RequestBody JSONObject data,String businessKey) {
		boolean flag = basicLoadService.update(data,businessKey);
		return flag ? R.success("修改成功") : R.fail("修改失败");
	}

	/**
	 *根据businessKey修改表单数据
	 * @return
	 */
	@PostMapping("/updateByBusinessKey")
	@ApiOperation("根据businessKey修改表单数据")
	public R updateByBusinessKey(@RequestBody JSONObject jsonObject,@RequestParam String businessKey){
		boolean flag = basicLoadService.updateByBusinessKey(jsonObject,businessKey);
		return flag ? R.success("修改成功") : R.fail("修改失败");
	}


	@GetMapping("/dataAnalyse")
	@ApiOperation("数据分析-项目总览")
	public R dataAnalyse(@RequestParam int year){
		JSONObject jsonObject = basicLoadService.dataAnalyse(year);
		return R.data(jsonObject);
	}



	@GetMapping("/newDataAnalyse")
	@ApiOperation("新数据分析-项目总览")
	public R newDataAnalyse(String startTime,String endTime){
		HashMap<String, Object> map = basicLoadService.newDataAnalyse(startTime,endTime);
		return R.data(map);
	}

}

